import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/Login.vue'
import {
  $_checktoken
} from '@/apis/user'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/Index',
    name: 'Index',
    component: () => import('../views/Index.vue'),
    children: [{
        path: 'Home',
        name: 'home',
        component: () => import('../views/modoules/Home.vue'),
      }, {
        path: 'Order',
        name: 'order',
        component: () => import('../views/modoules/Order.vue'),
      }, {
        path: 'Storemanage',
        component: () => import('../views/modoules/Storemanage.vue'),
      },

      {
        path: 'Goodslist',
        component: () => import('../views/modoules/Goodslist.vue'),
      }, {
        path: 'Goodsadd',
        component: () => import('../views/modoules/Goodsadd.vue'),

      }, {
        path: 'Goodssort',
        component: () => import('../views/modoules/Goodssort.vue'),

      },
      {
        path: 'Accountlist',
        component: () => import('../views/modoules/Accountlist.vue'),

      }, {
        path: 'Accountadd',
        component: () => import('../views/modoules/Accountadd.vue'),

      }, {
        path: 'Changepassword',
        component: () => import('../views/modoules/Changepassword.vue'),
      },
      {
        path: 'Goodstatistics',
        component: () => import('../views/modoules/Goodstatistics.vue'),
      }, {
        path: 'Orderstatistcs',
        component: () => import('../views/modoules/Orderstatistcs.vue'),
      }, {
        path: 'accountinfo',
        name: 'accountinfo',
        component: () => import('../views/modoules/Accountinfo.vue')
      }
    ]
  },
  {
    path: '*',
    component: () => import('../views/Error404.vue')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path == '/')
    next()
  else {
    $_checktoken({
      token: localStorage.token
    }).then(res => {

      if (res.data.code == 0) //有效
        next()
      else //无效
        next('/')
    })
  }
})


export default router